package com.lb.shopguide.ui.fragment.boss;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.shop.StaffBean;
import com.lb.shopguide.event.boss.AddGuideEvent;
import com.lb.shopguide.event.boss.RookieAddGuideEvent;
import com.lb.shopguide.http.method.ApiMethodBoss;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.baselib.http.SimpleObserverOtherListener;
import com.lb.shopguide.ui.dialog.DialogChooseStaffType;
import com.lb.shopguide.ui.view.ClearTitleBar;
import com.lb.shopguide.util.lb.BizUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * Created by kang on 2018/1/3.
 */

public class FragmentAddEditShopManager extends BaseCommonFragment {
    @BindView(R.id.ctb)
    ClearTitleBar ctb;
    @BindView(R.id.et_manager_nickname)
    EditText etManagerNickName;
    @BindView(R.id.et_manager_realname)
    EditText etManagerRealName;
    @BindView(R.id.et_manager_phone)
    EditText etManagerPhone;
    @BindView(R.id.tv_add_shop_manager)
    TextView btnAddShopManager;
    @BindView(R.id.tv_staff_position)
    TextView tvStaffPosition;
    @BindView(R.id.layout_staff_position)
    RelativeLayout layoutStaffPosition;

    private String storeCode;
    private int staffType = AppConstant.STATE_ADD_SHOP_GUIDE;
    private int type;
    private StaffBean staffBean;

    public static FragmentAddEditShopManager newInstance(String storeCode, int type) {
        FragmentAddEditShopManager fragmentAddShopManager = new FragmentAddEditShopManager();
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.STORE_CODE, storeCode);
        bundle.putInt(AppConstant.ADD_EDIT_SHOP_GUIDE_STATE, type);
        fragmentAddShopManager.setArguments(bundle);
        return fragmentAddShopManager;
    }

    public static FragmentAddEditShopManager newInstance(StaffBean staffBean, int type) {
        FragmentAddEditShopManager fragmentAddShopManager = new FragmentAddEditShopManager();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConstant.STAFF_BEAN, staffBean);
        bundle.putInt(AppConstant.ADD_EDIT_SHOP_GUIDE_STATE, type);
        fragmentAddShopManager.setArguments(bundle);
        return fragmentAddShopManager;
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_add_shop_manage;
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.STORE_CODE)) {
            storeCode = bundle.getString(AppConstant.STORE_CODE);
        }
        if (bundle.containsKey(AppConstant.ADD_EDIT_SHOP_GUIDE_STATE)) {
            type = bundle.getInt(AppConstant.ADD_EDIT_SHOP_GUIDE_STATE);
        }
        if (bundle.containsKey(AppConstant.STAFF_BEAN)) {
            staffBean = (StaffBean) bundle.getSerializable(AppConstant.STAFF_BEAN);
        }
    }

    @Override
    public void initView() {
        if (type == AppConstant.STATE_EDIT_SHOP_GUIDE) {
            ctb.setTvTitle("编辑员工");
            bindData();
            btnAddShopManager.setText("保存");
        } else if (type == AppConstant.STATE_ADD_SHOP_GUIDE) {
            ctb.setTvTitle("新建员工");
            tvStaffPosition.setText("店长");
        }

        ctb.setLeftImageSrc(R.drawable.iv_back);
        ctb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }


    @Override
    public String getPageTag() {
        return FragmentAddEditShopManager.class.getName();
    }

    private void bindData() {
        etManagerNickName.setText(staffBean.getStaffNickName());
        etManagerPhone.setText(staffBean.getMobileNum());
        etManagerRealName.setText(staffBean.getStaffName());
        if (staffBean.getStaffType() == AppConstant.STAFF_TYPE_MANAGER) {
            tvStaffPosition.setText("店长");
        } else if (staffBean.getStaffType() == AppConstant.STAFF_TYPE_GUIDE) {
            tvStaffPosition.setText("导购");
        }
    }

    @OnClick(R.id.tv_add_shop_manager)
    void addShopManager() {
        sendRequestAddShopManager();
    }

    @OnClick(R.id.layout_staff_position)
    void staffPosition() {
        final DialogChooseStaffType dialogChooseStaffType = DialogChooseStaffType.getDialogChooseChargeinType();
        dialogChooseStaffType.show(getFragmentManager(), getPageTag());
        dialogChooseStaffType.setChooseTypeListener(new DialogChooseStaffType.OnSetChooseTypeListener() {
            @Override
            public void onGuideClick() {
                dialogChooseStaffType.dismiss();
                staffType = AppConstant.STAFF_TYPE_GUIDE;
                tvStaffPosition.setText("导购");
            }

            @Override
            public void onManagerClick() {
                dialogChooseStaffType.dismiss();
                staffType = AppConstant.STAFF_TYPE_MANAGER;
                tvStaffPosition.setText("店长");
            }
        });
    }

    private void sendRequestAddShopManager() {
        RequestBody body = getRequestBody();
        if (body == null) {
            return;
        }
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {
            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == (ResponseCode.RESPONSE_SUCCESS)) {
                    if (type == AppConstant.STATE_EDIT_SHOP_GUIDE) {
                        ToastUtils.showShort("编辑员工成功");
                    } else {
                        ToastUtils.showShort("新增员工成功");
                    }
                    pop();
                    EventBus.getDefault().post(new AddGuideEvent());
                    EventBus.getDefault().post(new RookieAddGuideEvent());
                } else {
                    ToastUtils.showShort(response.getMessage());
                }
            }
        };
        SimpleObserverOtherListener errorListener = new SimpleObserverOtherListener() {
            @Override
            public void onSessionOutOfDate() {

            }

            @Override
            public void onError(String error) {
                if (type == AppConstant.STATE_EDIT_SHOP_GUIDE) {
                    ToastUtils.showShort("编辑员工失败");
                } else {
                    ToastUtils.showShort("新增员工失败");
                }
            }
        };
        ApiMethodBoss.addShopManager(new SimpleLbObserver(listener, errorListener), body,this, FragmentEvent.DESTROY_VIEW);
    }

    private RequestBody getRequestBody() {
        HashMap hashMap = new HashMap();
        String shopManagerNickName = etManagerNickName.getText().toString();
        String shopManagerRealName = etManagerRealName.getText().toString();
        String shopManagerPhone = etManagerPhone.getText().toString();

        if (TextUtils.isEmpty(shopManagerNickName)) {
            ToastUtils.showShort("店内昵称必须填写");
            return null;
        }
        if (TextUtils.isEmpty(shopManagerRealName)) {
            ToastUtils.showShort("店长姓名必须填写");
            return null;
        }
        if (TextUtils.isEmpty(shopManagerPhone)) {
            ToastUtils.showShort("店长手机必须填写");
            return null;
        } else {
            if (RegexUtils.isMobileExact(shopManagerPhone)) {
            } else {
                ToastUtils.showShort("请输入正确的手机号码");
                return null;
            }
        }

        hashMap.put("staffNickName", shopManagerNickName);
        hashMap.put("staffName", shopManagerRealName);
        hashMap.put("mobileNum", shopManagerPhone);
        hashMap.put("storeCode", storeCode);
        hashMap.put("staffType", staffType);
        if (type == AppConstant.STATE_EDIT_SHOP_GUIDE) {
            hashMap.put("id", staffBean.getId());
        } else if (type == AppConstant.STATE_ADD_SHOP_GUIDE) {
            hashMap.put("id", 0);
        }
        return BizUtil.getBodyFromMap(hashMap);
    }
}
